
$(function() {
	$("#type").change(function(form) {
		_type($(this).val());
    });

	_type($("#type").val());
	$("#vvv").val(enquiry_v());

	$("input[name^='v_']").change(function(form) {		
		// $("#price").val('');
		$("#vvv").val(enquiry_v());
    });
	
	_change("input[id='unit']:visible", "select[id^='unit_t']:visible");
	
	$('#density_t').change(function() {
		 var s = $(this).val();
		 if (s != null && s != '-')　{
			  $('#density').val(s);
		 }		
	 });
	
	$('#v_l').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#v_w').focus();
       }
	});
	
	$('#v_w').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#v_h1').focus();
       }
	});
	
	$('#v_d').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#v_h2').focus();
       }
	});
	
	$('#v_h1').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#density').focus();
       }
	});
	
	$('#v_h2').focus().keypress(function(event){
       if (event.keyCode == 13) {
    	   $('#density').focus();
       }
	});

	$('textarea').css("width", 400);
});

function enquiry_v() {
	var type = $("#type").val();
	if (type == 2) {
		return enquiry_v_2();
	}

	if (type == 3) {
		return enquiry_v_3();
	}

	return '';
}

function enquiry_v_2() {
	var v_l = $("#v_l").val();
	if (v_l == null || v_l == '') {
		return '';
	}

	var v_w = $("#v_w").val();
	if (v_w == null || v_w == '') {
		return '';
	}

	var v_h1 = $("#v_h1").val();
	if (v_h1 == null || v_h1 == '') {
		return '';
	}

	var r = eval(v_l) * eval(v_w) * eval(v_h1) * Math.pow(10, -3);

	return enquiry_fmoney(r, 3);
}

function enquiry_v_3() {
	var v_d = $("#v_d").val();
	if (v_d == null || v_d == '') {
		return '';
	}

	var b = eval(v_d) / 2;

	var v_h2 = $("#v_h2").val();
	if (v_h2 == null || v_h2 == '') {
		return '';
	}

	var r = Math.PI * Math.pow(b, 2) * eval(v_h2) * Math.pow(10, -3);

	return enquiry_fmoney(r, 3);
}

function enquiry_fmoney(s, n) {
   n = n > 0 && n <= 20 ? n : 2;
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
   var l = s.split(".")[0].split("").reverse();
   var r = s.split(".")[1];
   var t = "";
   for (i = 0; i < l.length; i ++ ) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "" : "");
   }

   return t.split("").reverse().join("") + "." + r;
}